import com.sun.source.tree.Tree;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }
    /**
     * 创建一棵二叉树 返回这棵树的根节点
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int nodeSize;
    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftN = size2(root.left);
        int rightN = size2(root.right);
        return leftN + rightN + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;
    void getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.right == null && root.left == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        } else if (root.left == null && root.right == null) {
            return 1;
        } else {
            return getLeafNodeCount2(root.left)
                    + getLeafNodeCount2(root.right);
        }
    }

    /*
    * 获取第K层节点的个数
    * 整棵树第k层的节点个数 = 左子树第k-1层的节点个数 + 右子树第k-1层的节点个数
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftV = find(root.left,val);
        if (leftV != null) {
            return leftV;
        }
        TreeNode rightV = find(root.right,val);
        if (rightV != null) {
            return rightV;
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

//    /**
//     * 二叉树的层序遍历
//     * @param root
//     * @return
//     */
//    public List<List<Integer>> levelOrder1(TreeNode root) {
//        //创建二维数组
//        List<List<Integer>> ret = new ArrayList<>();
//        if (root == null) return ret;
//        //创建队列
//        Queue<TreeNode> queue = new LinkedList<>();
//        //把根节点压入队列
//        queue.offer(root);
//        while (!queue.isEmpty()) {
//            // 例如：一层的数据，有1个
//            // 创建数组
//            List<Integer> curList = new ArrayList<>();
//            int count = queue.size();//count == 1
//            while (count != 0) {
//                TreeNode cur = queue.poll();
//                curList.add(cur.val);
//                if (cur.left != null) {
//                    queue.offer(cur.left);
//                }
//                if (cur.right != null) {
//                    queue.offer(cur.right);
//                }
//                count--;
//            }
//            ret.add(curList);
//        }
//        return ret;
//    }

//    /**
//     * 二叉树的层序遍遍历2，从最后一层开始打印
//     * @param root
//     * @return
//     */
//    public List<List<Integer>> levelOrderBottom(TreeNode root) {
//        List<List<Integer>> ret = new ArrayList<>();
//        if (root == null) return ret;
//        //创建队列
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//        while (!queue.isEmpty()) {
//            List<Integer> curList = new ArrayList<>();
//            int size = queue.size();
//            while (size != 0) {
//                TreeNode cur = queue.poll();
//                curList.add(cur.val);
//                if (cur.left != null) {
//                    queue.offer(cur.left);
//                }
//                if (cur.right != null) {
//                    queue.offer(cur.right);
//                }
//                size--;
//            }
//            ret.add(0,curList);
//        }
//        return ret;
//    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        return true;
    }

    /**
     * 判断一棵树是不是平衡二叉树
     * 时间复杂度：O(n^2)
     * @param root
     * @return
     */
    public boolean isBalanced1(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHigh = getHeight(root.left);
        int rightHigh = getHeight(root.right);
        return Math.abs(leftHigh - rightHigh) <= 1
                && isBalanced1(root.left)
                && isBalanced1(root.right);
    }
    /**
     * 判断一棵树是不是平衡二叉树
     * 时间复杂度：O(n)
     * @param root
     * @return
     */
    public boolean isBalanced2(TreeNode root) {
        if (root == null) {
            return true;
        }
        return getHeight(root) >= 0;
    }
    //求二叉树的高度
    int getHeight2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = getHeight2(root.left);
        int rightH = getHeight2(root.right);
        if (leftH >= 0 && rightH >= 0 && Math.abs(leftH - rightH) <= 1) {
            //这个子树是平衡二叉树
            return Math.max(leftH,rightH) + 1;
        } else {
            //-1表示不是平衡二叉树
            return -1;
        }
    }

    /**
     * 翻转二叉树
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    /**
     * 判断两棵树是否相同
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //一个空，一个不空
        if (p == null && q != null || p != null && q == null) {
            return false;
        }
        //两个都是空
        if (p == null && q == null) {
            return true;
        }
        //都不是空
        if (p.val != q.val) {
            return false;
        }
        //不为空，且val相同，去判断左右子树
        return  isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    /**
     * 判断是不是另一棵树的子树
     * @param root
     * @param subRoot
     * @return
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        //判断是不是相同的树
        if (isSameTree(root,subRoot)) {
            return true;
        }
        //判断左子树是不是子树
        if(isSubtree(root.left,subRoot)) {
            return true;
        }
        //判断右子树是不是子树
        if(isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是不是镜像对称二叉树
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
        //一个是空，一个不是空，不是对称的
        if (leftTree != null && rightTree == null ||
                rightTree != null && leftTree == null) {
            return false;
        }
        //两个都是空，是对称的
        if (leftTree == null && rightTree == null) {
            return true;
        }
        //两个都不是空，判断值
        if (leftTree.val != rightTree.val) {
            return false;
        }
        //非空，值也相同,去判断左子树和右子树是否是对称的
        return isSymmetricChild(leftTree.left,rightTree.right)
                && isSymmetricChild(leftTree.right,rightTree.left);
    }

//    //返回叶子节点个数
//    public int getLeafNodeCount(TreeNode root) {
//        if (root == null) {
//            return 0 ;
//        } else if (root.left == null && root.right == null) {
//            return 1;
//        } else {
//            return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
//        }
//    }

//    public int leafCount;
//    public void getLeafNodeCount1(TreeNode root) {
//        if (root == null) {
//            return;
//        }
//        if (root.left == null && root.right == null) {
//            leafCount++;
//        }
//        getLeafNodeCount1(root.left);
//        getLeafNodeCount1(root.right);
//  }

    /**
     * 二叉树的最近公共祖先节点
     * @param root
     * @param p
     * @param q
     * @return
     */
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root == p || root == q) {
            return root;
        }
        TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
        TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
        if (leftNode != null && rightNode != null) {
            return root;
        } else if(leftNode != null) {
            return leftNode;
        } else if(rightNode != null) {
            return rightNode;
        }
        return null;
    }


/*    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        //交换左右子树
        TreeNode cur = root.right;
        root.right = root.left;
        root.left = cur;
        //递归的去左子树和右子树找
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }*/


/*    *//**
     * 判断两棵树是否相同
     * @param p
     * @param q
     * @return
     *//*
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //两个都是空
        if (p == null && q == null) {
            return true;
        }
        //一个是空，一个不是空
        if (p == null && q != null || p != null && q == null) {
            return false;
        }
        //两个都不为空
        if (p.val != q.val) {
            return false;
        }
        //走到这里，不为空且值相同，去判断左子树和右子树
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }*/


/*    *//**
     * 对称二叉树
     * @param root
     * @return
     *//*
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left, root.right);
    }

    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
        //一边空，一边不空
        if (leftTree == null && rightTree != null || rightTree == null && leftTree != null) {
            return false;
        }
        //两边都空
        if (leftTree == null && rightTree == null) {
            return true;
        }
        //两边都不空,判断val值
        if (leftTree.val != rightTree.val) {
            return false;
        }
        //非空，值也相同，去判断左右子树是否对称
        return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
    }*/


/*    *//**
     * 判断是不是另一棵树的子树
     * @param root
     * @param subRoot
     * @return
     *//*
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        //判断两棵树是否相同
        if (isSameTree(root, subRoot)) {
            return true;
        }
        //判断是不是root的左子树
        if (isSubtree(root.left, subRoot)) {
            return true;
        }
        //判断是不是root的右子树
        if (isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }*/


    /**
     * 判断平衡二叉树
     * @param root
     * @return
     */
    //求树高度
    int getTreeHigh(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHigh = getTreeHigh(root.left);
        int rightHigh = getTreeHigh(root.right);
        return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;
    }
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHigh = getTreeHigh(root.left);
        int rightHigh = getTreeHigh(root.right);
        return Math.abs(leftHigh - rightHigh) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }
}

/**
 * 根据字符串创建二叉树！！！
 */
class Main {
    public static int i = 0;
    //创建树
    public static BinaryTree.TreeNode createTree(String str) {
        //创建根节点
        BinaryTree.TreeNode root = null;
        if (str.charAt(i) != '#') {
            root = new BinaryTree.TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }
        return root;
    }
    //中序遍历
    public static void inorder(BinaryTree.TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            BinaryTree.TreeNode root = createTree(str);
            inorder(root);
        }
    }
}
